//
//  XDExchangeViewController.m
//  XDEducation
//
//  Created by 星道 on 17/5/12.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "XDExchangeViewController.h"
#import "XDExchangeCell.h"

#import "XDExchangeModel.h"
@interface XDExchangeViewController ()

//表单数据
@property (nonatomic, strong) NSArray *staticArr;

@end

//CellID
static NSString *ExchangeCellID = @"ExchangeCell";

@implementation XDExchangeViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //初始化Table
    [self ConfigTable];
    
    //请求数据
    [self NetData];
}

/** 配置table */
- (void)ConfigTable
{
    [self.tableView registerNib:[UINib nibWithNibName:[XDExchangeCell description] bundle:nil] forCellReuseIdentifier:ExchangeCellID];
}


/** 网络数据请求 */
- (void)NetData
{
    [MBProgressHUD showHUD];
    [XDHttpTool get:BaseURL(@"myselfws.do?action=getInfoByUid") authCode:[AccountTool shareAccountTool].currentAccount.authCode params:nil success:^(id responseObj) {
        
        NSLog(@"%@",(NSString *)responseObj);
        if ([responseObj[@"code"] isEqualToNumber:@1000])
        {
            NSInteger StarStr = [responseObj[@"t"][@"money"] integerValue];
            NSInteger FlowerStr = [responseObj[@"t"][@"flowers"] integerValue];
            NSInteger HeartStr = [responseObj[@"t"][@"hearts"] integerValue];
            
            XDExchangeModel *StarModel = self.staticArr[0];
            StarModel.Quantity = StarStr;
            
            XDExchangeModel *FlowerModer = self.staticArr[1];
            FlowerModer.Quantity = FlowerStr;
            
            XDExchangeModel *HeartModel = self.staticArr[2];
            HeartModel.Quantity = HeartStr;
            
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tableView reloadData];
            });
            
        }
        
    } failure:^(NSError *error) {
        [MBProgressHUD showError:XDNetWorkError];
    }];
}


#pragma mark - TableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.staticArr count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    XDExchangeModel *Model = self.staticArr[indexPath.row];
    XDExchangeCell *cell = [tableView dequeueReusableCellWithIdentifier:ExchangeCellID forIndexPath:indexPath];
    cell.Model = Model;
    WeakSelf(WeakSelf);
    cell.ExChangeAction = ^(XDExchangeModel *Model)
    {
        //兑换事件
        [WeakSelf ExchangeAction:Model];
    };
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 64.4f;
}



#pragma mark - Action
/** 单击了兑换 */
- (void)ExchangeAction:(XDExchangeModel *)Model
{
    //星币数XBS 红花数HHS 爱心数AXS
    //红花500 爱心600
    //红花getStarsByFlowers 爱心getStarsByHeart
    if ([Model.Code isEqualToString:@"HHS"])    //红花
    {
        if (Model.Quantity >= 500)
        {
            [self ExchangeNet:@"getStarsByFlowers"];
        }
        else
        {
            [MBProgressHUD showError:@"红花数不够,无法兑换,至少要500"];
        }
    }
    else if ([Model.Code isEqualToString:@"AXS"])   //爱心
    {
        if (Model.Quantity >= 500)
        {
            [self ExchangeNet:@"getStarsByHeart"];
        }
        else
        {
            [MBProgressHUD showError:@"爱心数不够,无法兑换,至少要600"];
        }
    }
    else
    {
    }
}

/** 条件通过，开始兑换 */
- (void)ExchangeNet:(NSString *)ActionStr
{
    NSString *Url = [NSString stringWithFormat:@"myselfws.do?action=%@",ActionStr];
    [MBProgressHUD showHUD];
    [XDHttpTool get:BaseURL(Url) authCode:[AccountTool shareAccountTool].currentAccount.authCode params:nil success:^(id responseObj) {
        
       if ([responseObj[@"code"] isEqualToNumber:@1000])
       {
           [MBProgressHUD showSuccess:@"兑换成功"];
           
           //数据更新
           [self performSelector:@selector(NetData) withObject:nil afterDelay:1.0f];
       }
       else
       {
           [MBProgressHUD showError:@"兑换失败"];
       }
    } failure:^(NSError *error) {
        [MBProgressHUD showError:@"兑换失败"];
    }];
}



#pragma mark - Setter
- (NSArray *)staticArr
{
    if (!_staticArr)
    {
        XDExchangeModel *StarModel = [[XDExchangeModel alloc] init];
        StarModel.Code      = @"XBS";
        StarModel.ImgName   = @"exchange_icon_star_default";
        StarModel.Title     = @"星币数";
        StarModel.Quantity  = 0;
        StarModel.IsShow    = NO;
        StarModel.IsChange  = YES;
        
        XDExchangeModel *FlowerModel = [[XDExchangeModel alloc] init];
        FlowerModel.Code      = @"HHS";
        FlowerModel.ImgName   = @"exchange_icon_safflower_default";
        FlowerModel.Title     = @"红花数";
        FlowerModel.Quantity  = 0;
        FlowerModel.IsShow    = YES;
        FlowerModel.IsChange  = YES;
        
        XDExchangeModel *HeartModel = [[XDExchangeModel alloc] init];
        HeartModel.Code      = @"AXS";
        HeartModel.ImgName   = @"exchange_icon_Love_default";
        HeartModel.Title     = @"爱心数";
        HeartModel.Quantity  = 0;
        HeartModel.IsShow    = YES;
        HeartModel.IsChange  = YES;
        
        _staticArr = @[StarModel,FlowerModel,HeartModel];
    }
    return _staticArr;
}
@end
